package com.xiaoc.bulldozer.springboot.config.web;

import com.xiaoc.bulldozer.springboot.config.web.interceptor.UserSessionHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Bean
    UserSessionHandler userSessionHandler() {
        return new UserSessionHandler();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
//        registry.addResourceHandler("/views/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/views/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截规则：除了login，其他都拦截判断
        registry.addInterceptor(userSessionHandler()).addPathPatterns("/**").excludePathPatterns("/user/weixin/login", "/user/weixin/proxyLogin", "/loanOnline/updateLoanStatus");
    }

    @Bean
    @ConditionalOnProperty("log.response.enable")
    public FilterRegistrationBean addFilter(){
        FilterRegistrationBean filterBean = new FilterRegistrationBean();
        filterBean.setFilter(new XiaocCommonsRequestLoggingFilter());
        filterBean.setName("commonsRequestLoggingFilter");
        filterBean.addUrlPatterns("/*");
        return filterBean;
    }

}
